package com.alibaba.citrus.cr.unified.inventory.utils;

import com.alibaba.cloud.retail.item.api.model.search.scm.ScItemSearchReqDTO;
import com.alibaba.cloud.retail.item.api.model.search.scm.ScItemSearchRequest;
import com.alibaba.cloud.retail.item.api.model.search.scm.ScItemSearchResponse;
import com.alibaba.cloud.retail.item.api.service.scm.ScItemSearchService;
import com.alibaba.cz.base.model.item.scm.ScItemSDO;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.result.Result;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yuhw
 * @date: 2023年12月12日 20:37
 **/
@Component
public class ScItemConverter {

    @DubboReference(protocol = "dubbo")
    private ScItemSearchService scItemSearchService;

    public List<ScItemSDO> queryScItemList(List<String> goodsCodeList){
        if (CollectionUtils.isEmpty(goodsCodeList)){
            return Lists.newArrayList();
        }
        ScItemSearchRequest scItemSearchRequest = new ScItemSearchRequest();
        ScItemSearchReqDTO searchReqDTO = new ScItemSearchReqDTO();
        searchReqDTO.setOuterIdList(goodsCodeList);
        scItemSearchRequest.setSearchReqDTO(searchReqDTO);
        scItemSearchRequest.setPageSize(goodsCodeList.size());
        Result<ScItemSearchResponse> scItemSearchResponseResult = scItemSearchService.searchFromDB(scItemSearchRequest);
        return Optional.ofNullable(scItemSearchResponseResult).map(Result::getResult).map(ScItemSearchResponse::getSearchResult)
                .orElse(Lists.newArrayList());
    }

    public Map<String,ScItemSDO> getScItemSDOMap(List<String> goodsCodeList){
        return Nullable.stream(queryScItemList(goodsCodeList))
                .collect(Collectors.toMap(ScItemSDO::getOuterId, scItemSDO -> scItemSDO, (a, b) -> b));
    }

    public Map<String,ScItemSDO> getScItemSDOMapByIds(List<String> scItemIds){
        Map<String,ScItemSDO> map = Maps.newHashMap();
        if (CollectionUtils.isEmpty(scItemIds)){
            return map;
        }
        ScItemSearchRequest scItemSearchRequest = new ScItemSearchRequest();
        ScItemSearchReqDTO searchReqDTO = new ScItemSearchReqDTO();
        searchReqDTO.setScItemIds(scItemIds);
        scItemSearchRequest.setSearchReqDTO(searchReqDTO);
        scItemSearchRequest.setPageSize(scItemIds.size());
        Result<ScItemSearchResponse> scItemSearchResponseResult = scItemSearchService.searchFromDB(scItemSearchRequest);
        Optional.ofNullable(scItemSearchResponseResult).map(Result::getResult).map(ScItemSearchResponse::getSearchResult)
                .orElse(Lists.newArrayList())
                .stream().forEach(scItemSDO -> {
                    map.put(scItemSDO.getScItemId(),scItemSDO);
                });
        return map;
    }

}
